package cz.data.domain.masterdata.controller;

import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.masterdata.mapstruct.ModelColumnMapstruct;
import cz.data.domain.masterdata.model.dto.ModelColumnDto;
import cz.data.domain.masterdata.model.entity.ModelColumnEntity;
import cz.data.domain.masterdata.model.query.ModelColumnQuery;
import cz.data.domain.masterdata.model.vo.ModelColumnVo;
import cz.data.domain.masterdata.service.ModelColumnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"主数据模型列"})
@RestController
@RequestMapping("/columns")
public class ModelColumnController extends BaseController {

    @Autowired
    private ModelColumnService modelColumnService;

    @Autowired
    private ModelColumnMapstruct modelColumnMapstruct;

    @ApiOperation(value = "主数据模型列-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<ModelColumnVo> getModelColumnById(@PathVariable String id) {
        ModelColumnEntity modelColumnEntity = modelColumnService.getModelColumnById(id);
        return new SimpleDataResponse<>(modelColumnMapstruct.toVO(modelColumnEntity));
    }

    @ApiOperation(value = "主数据模型列-分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelColumnQuery", value = "查询实体modelColumnQuery", required = true, dataTypeClass = ModelColumnQuery.class)
    })
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<ModelColumnVo>> getModelColumnPage(ModelColumnQuery modelColumnQuery) {
        QueryWrapper<ModelColumnEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(modelColumnQuery.getModelId()), "model_id", modelColumnQuery.getModelId());
        queryWrapper.like(StringUtils.isNotBlank(modelColumnQuery.getColumnName()), "column_name", modelColumnQuery.getColumnName());
        IPage<ModelColumnEntity> page = modelColumnService.page(new Page<>(modelColumnQuery.getPageNum(), modelColumnQuery.getPageSize()), queryWrapper);
        List<ModelColumnVo> collect = page.getRecords().stream().map(modelColumnMapstruct::toVO).collect(Collectors.toList());
        JsonPage<ModelColumnVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "主数据模型列-添加")
    @ApiImplicitParam(name = "modelColumn", value = "详细实体modelColumn", required = true, dataType = "ModelColumnDto")
    @PostMapping()
    public SimpleDataResponse<ModelColumnVo> saveModelColumn(@RequestBody @Validated({ValidationGroups.Insert.class}) ModelColumnDto modelColumn) {
        ModelColumnEntity modelColumnEntity = modelColumnService.saveModelColumn(modelColumn);
        return new SimpleDataResponse<>(modelColumnMapstruct.toVO(modelColumnEntity));
    }

    @ApiOperation(value = "主数据模型列-修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "modelColumn", value = "详细实体modelColumn", required = true, dataType = "ModelColumnDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<ModelColumnVo> updateModelColumn(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) ModelColumnDto modelColumn) {
        ModelColumnEntity modelColumnEntity = modelColumnService.updateModelColumn(modelColumn);
        return new SimpleDataResponse<>(modelColumnMapstruct.toVO(modelColumnEntity));
    }

    @ApiOperation(value = "主数据模型列-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteModelColumnById(@PathVariable String id) {
        modelColumnService.deleteModelColumnById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "主数据模型列-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteModelColumnBatch(@PathVariable List<String> ids) {
        modelColumnService.deleteModelColumnBatch(ids);
        return BaseResponse.success();
    }
}
